/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.una.accidentmanagement.view.manipulator;

import br.com.una.accidentmanagement.model.Area;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author santosmgbh
 */
public class PersonalizableComboBoxModel extends DefaultComboBoxModel {
    
    private List objects;
    private String stringFieldToUse;

    public PersonalizableComboBoxModel(List objects, String stringFieldToUse) {
        try {
            this.objects = objects;
            this.stringFieldToUse = stringFieldToUse;
            pullDate();
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(PersonalizableComboBoxModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersonalizableComboBoxModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    
    private void pullDate() throws IllegalArgumentException, IllegalAccessException{             
        for(Object t: objects){            
            Field[] campos = t.getClass().getDeclaredFields();    
            for(Field f: campos){
                if(f.getName().equals(stringFieldToUse)){
                    f.setAccessible(true);                                
                    Object valor = f.get(t);
                    addElement(valor);
                    break;
                }
            }            
        }
    }
    
    
    
}
